﻿/*
*
* jQuery Slider Link Plugin v0.1
* Author        : Khalid Al-Kooheji (http://kkdev.blogspot.com)
* Version       : 0.1
* Last Modified : 22/03/2011
*
* Copyright 2011, Khalid Al-Kooheji
* Licensed under the MIT License (http://www.opensource.org/licenses/mit-license.php)
*
*/

$.extend($.fn, {
    linkSliders: function (options) {
        //type:restrict,dampen
        options = $.extend({}, { type: 'restrict', max: 100 }, options);
        var selector = $(this);

        $('<a></a>').insertAfter(selector);

        var totalVal = function () {
            var total_val = 0;
            selector.each(function () {
                total_val += $(this).slider('value');
            });
            return total_val;
        }

        var start_amount = 0;
        var start_total = 0;
        selector.bind("slidestart", function (event, ui) {
            start_amount = $(this).slider('value');
            start_total = totalVal();
        });

        /*selector.bind("slide", function (event, ui) {
            var total = totalVal();
            if (total > options.max) {
                var tdiff = total - options.max;

                var sdiff = (ui.value - start_amount);

                if (sdiff > tdiff) {
                    return false;
                }

            }

        });*/

        var _lock_slide = false;
        selector.bind("slidechange", function (event, ui) {
            if (_lock_slide == true) return;

            var total = totalVal();
            if (total > options.max) {
                if (options.type == 'restrict') {
                    _lock_slide = true;
                    $(this).slider('value', start_amount + (options.max - start_total));
                    _lock_slide = false;
                }
                if (options.type == 'dampen') {
                    var dec_amount = (total - options.max);
                    var total_without_this = 0;
                    selector.not(this).each(function (i) {
                        var val = $(this).slider('value');
                        total_without_this += val;
                    });
                    selector.not(this).each(function (i) {
                        var val = $(this).slider('value');
                        //if (val == 0) return;
                        _lock_slide = true;
                        var dec_perc = (val / total_without_this);
                        var dec = Math.ceil(dec_amount * dec_perc); // Math.min(val, Math.ceil(dec_amount * dec_perc));
                        $(this).slider('value', val - dec); //Math.ceil(dec_amount / (sliderCount - 1)));
                        _lock_slide = false;
                    });
                    //_lock_slide = true;
                    //$(this).slider('value', start_amount + (options.max - totalVal()));
                    // _lock_slide = false;
                }
            }

            $('#status').text(totalVal());
            $(this).next().text($(this).slider('value'));
        });
    }
});